﻿using UnityEngine;
using System.IO;
using ProtoBuf;

public class ProtoBufFileDataUtil : MonoBehaviour {

	/// <summary>
	/// Saves the file.
	/// </summary>
	/// <param name="data">Data.</param>
	/// <param name="path">Path.</param>
	/// <typeparam name="T">The 1st type parameter.</typeparam>
	public static void SaveFile<T>(T data,string path){
		using(Stream file = File.Create(path))
        {  
			Serializer.Serialize<T>(file, data);  
            file.Close();
        }
	}
	/// <summary>
	/// Loads the file.
	/// </summary>
	/// <returns>The file.</returns>
	/// <param name="path">Path.</param>
	/// <typeparam name="T">The 1st type parameter.</typeparam>
	public static T LoadFile<T>(string path){
		T df ;
		if(!File.Exists(path)){
			return default (T);
		}
		using(Stream file = File.OpenRead(path)){
			df = Serializer.Deserialize<T>(file);
            file.Close();
		}
		return df;
	}
	/// <summary>
	///  has or not has file in the specified path.
	/// </summary>
	/// <returns><c>true</c> if this instance has file the specified path; otherwise, <c>false</c>.</returns>
	/// <param name="path">Path.</param>
	public bool HasFile(string path){
		if(File.Exists(path)){
			return true;
		}else{
			return false;
		}
	}

}
